﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApCableExceptionMessageProvider
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.Flexibles;
using System;
using System.Text;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;


namespace DnProcessSimulateCommands;

internal class CApCableExceptionMessageProvider
{
  private static string CableActionToPrefixMessage(
    CApCableExceptionMessageProvider.CableAction action)
  {
    string prefixMessage = "";
    switch (action)
    {
      case CApCableExceptionMessageProvider.CableAction.CREATION:
        prefixMessage = CApFLEXStringTable.CABLE_CREATION_FAIL;
        break;
      case CApCableExceptionMessageProvider.CableAction.UPDATE:
        prefixMessage = CApFLEXStringTable.CABLE_UPDATE_FAIL;
        break;
      case CApCableExceptionMessageProvider.CableAction.OPTIMIZATION:
        prefixMessage = CApFLEXStringTable.CABLE_OPTIMIZATION_FAIL;
        break;
      case CApCableExceptionMessageProvider.CableAction.REGENERATION:
        prefixMessage = CApFLEXStringTable.CABLE_REGENERATION_FAIL;
        break;
    }
    return prefixMessage;
  }

  private static string FrameDescriptorToString(TxCableFrameDescriptor frameDescriptor)
  {
    string str = "";
    switch ((int) frameDescriptor.FrameType)
    {
      case 0:
        str = CApFLEXStringTable.ATTACHMENT_COLLISION_ERROR_START_FRAME;
        break;
      case 1:
        str = CApFLEXStringTable.ATTACHMENT_COLLISION_ERROR_END_FRAME;
        break;
      default:
        if (frameDescriptor.FrameObject != null)
        {
          str = $"\"{((ITxObject) frameDescriptor.FrameObject).Name}\"";
          break;
        }
        break;
    }
    return str;
  }

  public static string ComputeCableExceptionErrorMessage(
    TxException exception,
    CApCableExceptionMessageProvider.CableAction action)
  {
    string exceptionErrorMessage;
    switch (exception)
    {
      case TxCableNoValidCollisionFreeRouteException _:
        exceptionErrorMessage = CApCableExceptionMessageProvider.ComputeNoCollisionFreeRouteErrorMessage(action);
        break;
      case TxCableParameterOptimizationFailedException _:
        exceptionErrorMessage = CApCableExceptionMessageProvider.ComputeParameterOptimizationErrorMessage(action);
        break;
      case TxCableAttachmentCollisionException exception1:
        exceptionErrorMessage = CApCableExceptionMessageProvider.ComputeAttachmentCollisionErrorMessage(exception1, action);
        break;
      case TxArgumentException exception2:
        exceptionErrorMessage = CApCableExceptionMessageProvider.ComputeCableArgumentExceptionErrorMessage(exception2, action);
        break;
      case TxNoLicenseException _:
        exceptionErrorMessage = CApCableExceptionMessageProvider.ComputeCableLicenseExceptionErrorMessage(action);
        break;
      default:
        exceptionErrorMessage = CApCableExceptionMessageProvider.ComputeDefaultGenerationErrorMessage(action);
        break;
    }
    return exceptionErrorMessage;
  }

  public static string ComputeDefaultGenerationErrorMessage(
    CApCableExceptionMessageProvider.CableAction action)
  {
    return string.Format(CApCableExceptionMessageProvider.CableActionToPrefixMessage(action), (object) CApFLEXStringTable.CABLE_DEFAULT_GENERATION_FAIL);
  }

  private static string ComputeNoCollisionFreeRouteErrorMessage(
    CApCableExceptionMessageProvider.CableAction action)
  {
    return string.Format(CApCableExceptionMessageProvider.CableActionToPrefixMessage(action), (object) CApFLEXStringTable.CABLE_NO_COLLISION_FREE_ROUTE_FAIL);
  }

  private static string ComputeParameterOptimizationErrorMessage(
    CApCableExceptionMessageProvider.CableAction action)
  {
    return string.Format(CApCableExceptionMessageProvider.CableActionToPrefixMessage(action), (object) CApFLEXStringTable.CABLE_PARAMETER_OPTIMIZATION_FAIL);
  }

  private static string ComputeAttachmentCollisionErrorMessage(
    TxCableAttachmentCollisionException exception,
    CApCableExceptionMessageProvider.CableAction action)
  {
    string prefixMessage = CApCableExceptionMessageProvider.CableActionToPrefixMessage(action);
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.AppendLine(CApFLEXStringTable.ATTACHMENT_COLLISION_ERROR_PREAMBLE);
    stringBuilder.AppendLine("");
    foreach (TxCableFrameDescriptor conflictingFrame in exception.ConflictingFrames)
    {
      string str1 = "";
      int num = 0;
      foreach (ITxObject itxObject in exception.GetConflictingObstaclesForFrame(conflictingFrame))
      {
        string str2 = "";
        string str3 = $"\"{itxObject.Name}\"";
        if (num != 0)
          str2 = num != exception.CountConflictingObstaclesForFrame(conflictingFrame) - 1 ? ", " : $" {CApFLEXStringTable.ATTACHMENT_COLLISION_ERROR_AND} ";
        str1 = str1 + str2 + str3;
        ++num;
      }
      stringBuilder.AppendLine(string.Format(CApFLEXStringTable.ATTACHMENT_COLLISION_ERROR_TOO_CLOSE, (object) CApCableExceptionMessageProvider.FrameDescriptorToString(conflictingFrame), (object) str1));
    }
    stringBuilder.AppendLine("");
    stringBuilder.AppendLine(CApFLEXStringTable.ATTACHMENT_COLLISION_ERROR_WORKAROUND);
    return string.Format(prefixMessage, (object) stringBuilder.ToString());
  }

  private static string ComputeCableArgumentExceptionErrorMessage(
    TxArgumentException exception,
    CApCableExceptionMessageProvider.CableAction action)
  {
    return string.Format(CApCableExceptionMessageProvider.CableActionToPrefixMessage(action), (object) ((Exception) exception).Message);
  }

  private static string ComputeCableLicenseExceptionErrorMessage(
    CApCableExceptionMessageProvider.CableAction action)
  {
    return string.Format(CApCableExceptionMessageProvider.CableActionToPrefixMessage(action), (object) CApFLEXStringTable.CABLE_LICENSE_CHECK_FAIL);
  }

  public enum CableAction
  {
    CREATION,
    UPDATE,
    OPTIMIZATION,
    REGENERATION,
  }
}
